package qc.module.ehs.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.EhsAccidentLevelEnum;
import qc.common.core.enums.ResourceStatusFlagEnum;
import qc.module.ehs.dto.accidentrecord.AccidentRecordDto;
import qc.module.ehs.entity.EhsAccidentRecord;

import java.util.Date;
import java.util.List;

/**
 * EhsAccidentRecordMapper
 *
 * @author QuCheng Tech
 * @create 2024/10/29
 */
@Mapper
public interface EhsAccidentRecordMapper {
    EhsAccidentRecordMapper MAPPER = Mappers.getMapper(EhsAccidentRecordMapper.class);

    /**
     * entity -> dto
     * @param en
     * @return
     */
    @Mapping(target = "tm", source = "tm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "lvl", source = "lvl.index")
    AccidentRecordDto toDto(EhsAccidentRecord en);

    /**
     * List<entity> -> List<dto>
     * @param ens
     * @return
     */
    List<AccidentRecordDto> toDtoList(List<EhsAccidentRecord> ens);

    /**
     * dto -> entity
     * @param dto
     * @return
     */
    @Mapping(target = "tm", source = "tm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    EhsAccidentRecord toEntity(AccidentRecordDto dto);

    /**
     * 新增或修改dto -> entity
     */
    default EhsAccidentRecord dtoToEntity(AccidentRecordDto dto) {
        // 1.dto转为entity
        EhsAccidentRecord en = EhsAccidentRecordMapper.MAPPER.toEntity(dto);

        // 处理事故等级枚举，非空时进行转换，空值时使用默认        
        en.setLvl(getEhsAccidentLevelEnum(dto.getLvl()));

        return en;
    }

    static EhsAccidentLevelEnum getEhsAccidentLevelEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (EhsAccidentLevelEnum item : EhsAccidentLevelEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        
        return null;
    }
}
